<template>
  <div class="app-container">
    <div style="width: 100%; margin-bottom: 10px">
      <el-button type="primary" size="mini" @click="addCommand" v-if="detailData.status!=='已送达'">添加</el-button>
      <el-button type="primary" plain size="mini" @click="createExamine" v-if="detailData.type==='点餐网上订单'">生成留样记录</el-button>
    </div>
    <el-table border :data="detailList" style="width: 100%">
      <el-table-column label="编号" width="50" type="index" align="center" fixed="left"
                       :index="index=>index+1"/>

      <el-table-column prop="goodsName" label="菜品名称" align="center" min-width="180">
        <template #default="scope">
          <span v-show="!scope.row.editFlag">{{scope.row.goodsName}}</span>
          <el-select v-show="scope.row.editFlag" v-model="scope.row.normalId" placeholder="请选择点餐菜单" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in goodsOptions" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="count" label="数量" align="center" width="120">
        <template #default="scope">
          <span v-show="!scope.row.editFlag">{{scope.row.count}}</span>
          <el-input v-show="scope.row.editFlag"
                    v-model="scope.row.count">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="单价" prop="price" align="center" width="120"/>
      <el-table-column label="创建人" prop="createBy" width="120" align="center"/>
      <el-table-column label="更新人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" width="140">
        <template #default="scope">
          <div style="" v-if="detailData.status!=='已送达'">
            <el-button size="small"
                       type="text"
                       icon="CircleCheck"
                       v-show="scope.row.editFlag"
                       @click="submit(scope.row, $index)">保存
            </el-button>
            <el-button size="small"
                       type="text"
                       icon="CircleClose"
                       v-show="scope.row.editFlag"
                       @click="cancel(scope.row, $index)">取消
            </el-button>
            <el-button size="small"
                       icon="Edit"
                       type="text"
                       v-show="!scope.row.editFlag"
                       @click="edit(scope.row, $indx)"> 编辑
            </el-button>
            <el-button size="small"
                       icon="Delete"
                       type="text"
                       v-show="!scope.row.editFlag"
                       @click="del(scope.row, $index)">删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>


<script>
import {listAll, add, update, del} from "@/api/order/normal/detail";
import { listAll as listAllNormalGoods } from "@/api/shop/goods/normal";
import {createExamineGoods} from "@/api/examine/example/example";
import {get} from "@/api/order/normal/online";

export default {
  name: "GarbageRecordDetail",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      id: undefined,
      // orderType:'',
      // 遮罩层
      loading: true,
      // 菜品明细表格数据
      detailList: [],
      goodsOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailData:''
    };
  },
  created() {
    this.id = this.$route.query && this.$route.query.id;
    // this.orderType=this.$route.query && this.$route.query.orderType;
    this.getList();
    get(this.id).then(res=>{
      this.detailData=res.data
    })
    listAllNormalGoods({}).then(res => {
      this.goodsOptions = [];
      for (var i = 0; i < res.rows.length; i++) {
        this.goodsOptions.push({
          label: res.rows[i].name,
          value: res.rows[i].id
        });
      }
    });
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAll({
        orderId: this.id,
      }).then(response => {
          this.detailList = response.rows;
          this.loading = false;
        }
      );
    },
    addCommand: function() {
      var detail = {
        orderId: this.id
      }
      add(detail).then(res => {
        this.getList();
      })
    },
    submit: function(row, index) {
      let updateData = {
        id: row.id,
        normalId: row.normalId,
        count: row.count,
        orderId: row.orderId
      }
      update(updateData).then(res => {
        this.$modal.msgSuccess("更新成功");
        row.editFlag = false;
        this.getList();
      })
    },
    cancel: function(row, index) {
      row.editFlag = false;
      this.getList();
    },
    edit: function(row, index) {
      row.editFlag = true;
      this.$set(this.detailList, index, row);
    },
    del: function(row, index) {
      del(row.id).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      });
    },
    //生成留样记录
    createExamine(){
      this.$modal.confirm('将根据明细生成菜品留样单记录，是否确定').then(()=> {
        createExamineGoods({id:this.id}).then(res=>{
          this.$message.success('生成成功')
        })
      })
    }
  }
};
</script>
